package com.clock.album;

import com.clock.album.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.TableLayout;
import ohos.agp.components.Text;
import ohos.data.resultset.ResultSet;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.photokit.common.AVLogCompletedListener;
import ohos.media.photokit.metadata.AVLoggerConnection;
import ohos.media.photokit.metadata.AVStorage;
import ohos.security.SystemPermission;
import ohos.utils.net.Uri;

import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainAbility extends Ability {
   private String[] permission = new String[]{SystemPermission.CAMERA,SystemPermission.WRITE_USER_STORAGE,SystemPermission.READ_USER_STORAGE,SystemPermission.WRITE_MEDIA,SystemPermission.READ_MEDIA};
    private TableLayout ivView;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        requestPermissionsFromUser(permission,10086);
        ivView= (TableLayout) findComponentById(ResourceTable.Id_show_view);
        findComponentById(ResourceTable.Id_choose).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent1 = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withBundleName("com.clock.album")
                        .withAbilityName("com.clock.album.ListFileAbility")
                        .build();
                intent1.setOperation(operation);
                startAbilityForResult(intent1,10086);
            }
        });
//        super.setMainRoute(MainAbilitySlice.class.getName());
    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        super.onAbilityResult(requestCode, resultCode, resultData);
        switch (requestCode){
            case 10086:
                if(resultData!=null){
                    ArrayList<Integer> list=resultData.getIntegerArrayListParam("datalist");
                    if(list.size()>0){
                        getUITaskDispatcher().asyncDispatch(new Runnable() {
                            @Override
                            public void run() {
                                ivView.removeAllComponents();
                                for(int i=0;i<list.size();i++){
                                    Image image = new Image(MainAbility.this);
                                    image.setWidth(250);
                                    image.setHeight(250);
                                    image.setMarginsLeftAndRight(10, 10);
                                    image.setMarginsTopAndBottom(10, 10);
                                    image.setScaleMode(Image.ScaleMode.CLIP_CENTER);
                                    image.setPixelMap(getPixelMapById(list.get(i)+""));
                                    ivView.addComponent(image);
                                }
                            }
                        });
                    }
                }
                break;
        }
    }
    private PixelMap getPixelMapById(String id){
        DataAbilityHelper helper = DataAbilityHelper.creator(this);
        PixelMap pixelMap = null;
        ImageSource imageSource = null;
        Uri uri = Uri.appendEncodedPathToUri(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI, id);
        FileDescriptor fd = null;
        try {
            fd = helper.openFile(uri, "r");
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            imageSource = ImageSource.create(fd, null);
            pixelMap = imageSource.createPixelmap(null);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (imageSource != null) {
                imageSource.release();
            }
        }
        return pixelMap;
    }
}
